<?php
/**
 * 根据麦子学院微信开发高级教程
 * 练习消息群发接口使用
 */

namespace Home\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;

class MassController extends Controller {
    private $appid = 'wxb70359eb19266f10';
    private $appsecret = 'd05e862df76a7401e0b2af0f06e8eade';
    private $WechatAuth = '';
    private $access_token = '';


    public function __construct(){
        parent::__construct();
        if(!session('token')){
            $this->WechatAuth = new WechatAuth($this->appid, $this->appsecret); //初始化WechatAuth类
            $WechatAuth = $this->WechatAuth;
            $token = $WechatAuth->getAccessToken(); //获取access_token数组
            session(array('expire'=>$token['expire_in'])); //设置缓存时间
            session('token', $token['access_token']); //缓存token
            $this->access_token = $token['access_token'];
        }else{
            $token = session('token');
            $this->WechatAuth = new WechatAuth($this->appid, $this->appsecret, $token);
            $this->access_token = $token;
        }
        
    }

    public function send(){
        $WechatAuth = $this->WechatAuth;
        $user = M('Users')->find(5);
        $openid = $user['openid'];
        $content = '预览群发消息';
        $msgtype = 'text';
        $arr = $WechatAuth->messageMassPreview($openid, $content, $msgtype);
        var_dump($arr);
    }

}